package cc.webmania.android.akeletbolcsessege;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;

public class BolcsessegWidget extends AppWidgetProvider {

	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
		//Log.d("aKeletB", "Widget onUpdate");
		try {
			final int N = appWidgetIds.length;
			for (int i = 0; i < N; i++) {
				int appWidgetId = appWidgetIds[i];
				
				// Create a Remote View
				RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.bolcsesseg_widget);
								
				// Update the UI.
				DBHelper db = new DBHelper(context);
				Bolcsesseg bolcsesseg = db.getBolcsesseg(0);
				views.setTextViewText(R.id.widget_text, bolcsesseg.getBolcsesseg() + "\n\n - " + bolcsesseg.getForras() + " - ");
				
				// Create an Intent to launch ExampleActivity
	            Intent intent = createIntent(context);
	            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
	            views.setOnClickPendingIntent(R.id.aWidget, pendingIntent);

				// Notify the App Widget Manager to update the widget using the modified remote view.
				appWidgetManager.updateAppWidget(appWidgetId, views);				
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			//Log.d("aKeletB", "Widget onUpdate Exception");
			e.printStackTrace();
		}
	}

	protected Intent createIntent(Context context) {
		return new Intent(context, AKeletBolcsessege.class);
	}
}
